﻿var customGrid = function (id) {
    this.id = id;

    this.getCells = function (uniqueName) {
        return $('#' + id + ' td[UniqueName=' + uniqueName + ']');
    }

    this.getSelectedCells = function (uniqueName) {
        return $('#' + id + ' .gridRowSelected' + ' td[UniqueName=' + uniqueName + ']');
    }

    this.getSelectedDataItems = function () {
        var index = 0;
        var items = new Array();
        $('#' + id + ' .gridRowSelected').each(function () {
            var dataItem = $(this).attr('DataItem') != undefined ? eval($(this).attr('DataItem')) : null;
            items[index++] = dataItem;
        });
        return items;
    }

    function checkSelectAll() {
        var allChecked = true;
        $('#' + id + ' .select input[type=checkbox]').each(function () {
            if (this.checked == false)
                allChecked = false;
        });

        $('#' + id + ' .selectAll input[type=checkbox]:first-child').each(function () {
            this.checked = allChecked;
        });
    }

    this.init = function () {
        $('#' + id + ' tr').each(function () {
            $(this).find('td:last').css('border', '0px none transparent');
            $(this).find('th:last').css('border', '0px none transparent');
        });

        //$('#' + id + ' tr:nth-child(1)').addClass('gridHeader');

        $('#' + id + ' tr:even').addClass('gridRowEven').hover(
            function () {
                if ($(this).attr('class').toString().startsWith('gridHeader'))
                    return;
                if ($(this).attr('class').toString().startsWith('gridFooter'))
                    return;
                $(this).addClass('gridRowMouseHover');
                $(this).removeClass('gridRowEven');
            },

            function () {
                if ($(this).attr('class').toString().startsWith('gridHeader'))
                    return;
                if ($(this).attr('class').toString().startsWith('gridFooter'))
                    return;
                $(this).addClass('gridRowEven');
                $(this).removeClass('gridRowMouseHover');
            }
        );

        $('#' + id + ' tr:odd').addClass('gridRowOdd').hover(
                function () {
                    if ($(this).attr('class').toString().startsWith('gridHeader'))
                        return;
                    if ($(this).attr('class').toString().startsWith('gridFooter'))
                        return;
                    $(this).addClass('gridRowMouseHover');
                    $(this).removeClass('gridRowOdd');
                },

            function () {
                if ($(this).attr('class').toString().startsWith('gridHeader'))
                    return;
                if ($(this).attr('class').toString().startsWith('gridFooter'))
                    return;
                $(this).addClass('gridRowOdd');
                $(this).removeClass('gridRowMouseHover');
            }
        );

        $('#' + id + ' tr').click(function () {
            if ($(this).attr('class').toString().startsWith('gridHeader'))
                return;

            if ($(this).attr('class').toString().startsWith('gridFooter'))
                return;

            var row = $(this);
            var dataItem = row.attr('DataItem') != undefined ? eval(row.attr('DataItem')) : null;

            if ($('#' + id).attr("AllowSelect") == 'true') {
                //clear all previous selected rows
                $('#' + id + ' .gridRowSelected .select input[type=checkbox]').each(function () {
                    this.checked = false;
                });
                $('#' + id + ' .gridRowSelected').removeClass('gridRowSelected');

                //hightlight selected row
                $(this).find('.select input[type=checkbox]').each(function () {
                    this.click();
                });
                $(this).addClass('gridRowSelected');

                //call client event handler
                var func_rowClick = $('#' + id).attr('OnClientRowClick');
                if (func_rowClick != undefined) {
                    window[func_rowClick](row, dataItem);
                }

                //check whether select all checkbox is checked
                checkSelectAll();
            }
        });

        $('#' + id + ' tr td input').click(function (event) {
            agent = jQuery.browser;
            if (agent.msie) {
                window.event.cancelBubble = true;
            } else {
                event.stopPropagation();
            }
        });

        $('#' + id + ' tr td select').click(function (event) {
            agent = jQuery.browser;
            if (agent.msie) {
                window.event.cancelBubble = true;
            } else {
                event.stopPropagation();
            }
        });

        $('#' + id + ' .select input[type=checkbox]').change(function (event) {
            if (this.checked)
                $(this).parent().parent().parent().addClass('gridRowSelected');
            else
                $(this).parent().parent().parent().removeClass('gridRowSelected');

            checkSelectAll();
        });

        $('#' + id + ' .selectAll input[type=checkbox]:first-child').change(function (event) {
            var allChecked = this.checked;
            $('#' + id + ' .select input[type=checkbox]').each(function () {
                this.checked = allChecked;
                if (this.checked)
                    $(this).parent().parent().parent().addClass('gridRowSelected');
                else
                    $(this).parent().parent().parent().removeClass('gridRowSelected');
            });
        });
    }
}